package com.aerozhonghuan.logisticsmall.modules.home.search;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.DB.DBAdapter;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarFragment;
import com.aerozhonghuan.logisticsmall.modules.common.entity.GoodsObject;
import com.aerozhonghuan.logisticsmall.modules.home.ChangeProductGouwucheNum;
import com.aerozhonghuan.logisticsmall.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ChenLin on 2019/5/22 10:01.Email&WeChat:7877409@163.com
 */
public class SearchProductFragment extends TitlebarFragment implements ChangeProductGouwucheNum {

    public static final String TAG = SearchProductFragment.class.getSimpleName();
    public static SearchAcrivity mSearchProductActivity;
    private boolean DEBUG = com.blankj.utilcode.util.AppUtils.isAppDebug();
    private List<GoodsObject> mGoodsObjects = new ArrayList<GoodsObject>();
    //private MyAdapter mAdapter;
    private ProductListAdapter mProductListAdapter;
    private DBAdapter mDbAdapter;
    private GridView mProductGridView;
    private int mSourceId;
    private View rootView;
    private LinearLayout llNoView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_search_result, null);
            StatusBarUtil.setLightStatusBar(getActivity(), true);
            mDbAdapter = new DBAdapter(getContext());
            initView();
            initData();
        }
        return rootView;
    }

    private void initView() {
        mSearchProductActivity = (SearchAcrivity)getActivity();
        mProductGridView = (GridView) rootView.findViewById(R.id.product_gridview);
        llNoView = (LinearLayout) rootView.findViewById(R.id.ll_no);
    }

    private void initData() {
        mGoodsObjects = (ArrayList<GoodsObject>) getArguments().getSerializable("search_res");
        if (mGoodsObjects != null && mGoodsObjects.size() > 0) {
            llNoView.setVisibility(View.GONE);
            mSourceId = 3;
            mProductListAdapter = new ProductListAdapter(getActivity(), mDbAdapter, "search_product_activity", mSourceId);
            mProductGridView.setAdapter(mProductListAdapter);
        } else {
            llNoView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        ArrayList<GoodsObject> gouwucheList = mDbAdapter.getAllData();
        mProductListAdapter.setData(reSetValue(gouwucheList, mGoodsObjects));
    }

    private List<GoodsObject> reSetValue(List<GoodsObject> gouwucheList, List<GoodsObject> list) {
        if (gouwucheList == null || gouwucheList.size() == 0) {
            return list;
        }
        if (DEBUG) {
            Log.d("community2", "gouwuchesize:" + gouwucheList.size() + "   list.size:" + list.size());
        }
        for (GoodsObject gouwucheP : gouwucheList) {
            for (GoodsObject p : list) {
                if (gouwucheP.getPvsid() == p.getPvsid()) {
                    int intoGouwucheSize = gouwucheP.getIntoGouwucheSize();
                    p.setIntoGouwucheSize(intoGouwucheSize);
                }
            }
        }
        mGoodsObjects = list;
        return list;
    }

    @Override
    public void change(GoodsObject product) {
        if (mGoodsObjects == null) {
            return;
        }
        for (GoodsObject p : mGoodsObjects) {
            if (p.getPvsid() == product.getPvsid()) {
                p.setIntoGouwucheSize(product.getIntoGouwucheSize());
            }
        }
        mProductListAdapter.setData(mGoodsObjects);
    }
}
